/*******************************************************************************
 * Copyright (c) 2008-2009 zhang yuexiang.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package org.xfeep.alaya.orm.jpa;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement(name="persistence")
@XmlAccessorType(XmlAccessType.FIELD)
public class PersistenceUnitInfoGroup {
	
	@XmlAttribute
	public String version;
	
	@XmlElements(@XmlElement(name="persistence-unit", type=PersistenceUnitInfoImp.class))
	public List<PersistenceUnitInfoImp> persistenceUnitInfoList = new ArrayList<PersistenceUnitInfoImp>();
	

	public void evalPropertiesAndJarUrlForAllUnits(URL rootUrl) throws MalformedURLException {
		//jar file e.g. jar:file:/c:/almanac/my.jar!/com/mycompany/MyClass.class
//		String rootUrlString = rootUrl.toString();
//		if ( rootUrlString.endsWith(".jar") ) {
//			rootUrlString +=   "!"; 
//		}
		
		for (PersistenceUnitInfoImp pui : persistenceUnitInfoList){
			pui.schemaVersion = version;
			pui.evalProperties();
			pui.evalJarFileUrls(rootUrl.toString());
			pui.persistenceUnitRootUrl = rootUrl;
			//TODO: we should give a property tmp ClassLoader 
			pui.classLoader = pui.newTempClassLoader = Thread.currentThread().getContextClassLoader();
		}
	}
	
	public static JAXBContext jaxbContext = null;
	
	
	public static JAXBContext getJaxbContext() {
		if (jaxbContext == null){
			try {
				jaxbContext = JAXBContext.newInstance(PersistenceUnitInfoGroup.class, PersistenceUnitInfoImp.class);
			} catch (JAXBException e) {
				throw new RuntimeException("can not create JAXBContext", e);
			}
		}
		return jaxbContext;
	}
	
	public static PersistenceUnitInfoGroup parse(InputStream xmlStream) throws JAXBException {
		return (PersistenceUnitInfoGroup)getJaxbContext().createUnmarshaller().unmarshal(xmlStream);
	}
	
	public static PersistenceUnitInfoGroup parse(URL xml) throws IOException {
		try{
			String rootUrlString = xml.toString();
			rootUrlString = rootUrlString.substring(0, rootUrlString.length() - "META-INF/persistence.xml".length());
			PersistenceUnitInfoGroup rt = (PersistenceUnitInfoGroup)getJaxbContext().createUnmarshaller().unmarshal(xml);
			rt.evalPropertiesAndJarUrlForAllUnits(new URL(rootUrlString));
			return rt;
		}catch (JAXBException e) {
			throw new IOException("parese error for " + xml + ", " + e.getMessage());
		}
	}
	
	public static PersistenceUnitInfoGroup parse(File xml) throws IOException {
		return parse(xml.toURI().toURL());

	}
	
}
